﻿
@{
    ViewData["Title"] = "AddOrderIndex";
    Layout = "~/Views/Share/_LayoutBody.cshtml";
}
<style>
    #bottomField {
        position: fixed;
        top: 0;
        right: 25px;
        background-color: rgba(0, 0, 0, 0.1);
        border-radius: 0px 0px 6px 6px;
        padding: 10px;
    }

   #bottomField .btn-group {
            font-size: 14px;
            margin-top: -1px;
            vertical-align: middle;
            color: #666666;
        }
    .layui-form-label {
        width: 80px;
        padding: 2px 10px 0px 0px;
    }
    .ui-jqgrid .ui-jqgrid-btable tbody tr.jqgrow td {
        padding-top: 5px;
        padding-bottom: 5px;
        line-height: 20px;
    }
    /*.layui-btn-xs {
        height: 20px;
        line-height: 20px;
    }
    .form-control {
        height: 20px;
    }
    .layui-btn-xs {
        height: 20px;
        line-height: 20px;
    }*/
</style>
<div class="layui-fluid">
    <div class="layui-row layui-col-space10">
        <div class="layui-col-xs12">
            <div class="layui-card">
                <div  class="layui-card-body" style=" height:500px; margin-bottom:20px;box-shadow: 0 0 3px rgba(0, 0, 0, 0.5);">
                    <form class="layui-form">
                        <div class="layui-form-item" style="margin-bottom:5px">
                            <label class="layui-form-label">单据日期</label>
                            <div class="layui-input-inline" style="width:100px">
                                <input type="text" name="BillDate" class="layui-input" id="BillDate">
                            </div>
                            <label class="layui-form-label">采购员</label>
                            <div class="layui-input-inline" style="width:140px">
                                <select id="CGName" name="CGName" lay-search lay-verify="required">
                                    <option value="">==请选择==</option>
                                </select>
                            </div>
                            <label class="layui-form-label">供应商</label>
                            <div class="layui-input-inline" style="width:140px">
                                <select id="SupName" name="SupName" lay-search lay-verify="required">
                                    <option value="">==请选择==</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">备注</label>
                            <div class="layui-input-inline">
                                <input type="text" id="BZ" name="BZ"  class="layui-input" style="width:560px;">
                            </div>

                        </div>
                    </form>

                    <table id="list1" class="table table-condensed"></table>                
                </div>
            </div>
        </div>
    </div>
</div>
<div id="bottomField">
    <button type="button" authorize="yes" class="layui-btn layui-btn-sm" data-type="SaveAdd" id="SaveAdd"><i class="layui-icon">&#xe61f;</i>保存并新增</button>
    <button type="button" authorize="yes" class="layui-btn layui-btn-sm" data-type="Add" id="Add"><i class="layui-icon ">&#xe608;</i>保存</button>
    <button type="button" authorize="yes" class="layui-btn layui-btn-sm" data-type="AddSH" id="AddSH"><i class="layui-icon ">&#xe672;</i>保存并审核</button>
</div>

<script>
    
    var lastrow;  //最后修改行号先定义一个全局变量
    var lastcell; //最后修改列号先定义一个全局变量
    //初始化界面
    layui.use(['table', 'form', 'layedit', 'laydate'], function () {
        var strdate = new Date().Format("yyyy-MM-dd");
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , table = layui.table;
        laydate.render({
            elem: '#BillDate',//制定元素
            type: 'date',
            show: false,//默认显示
            position: 'fixed',
            value: strdate,
            isInitValue: true, //是否允许填充初始值，默认为 true
           
        });
        //采购员
        $("#CGName").bindSelect({
            url: "/OrderBill/GetUser",
            id: "id",
            text: "UserName"
        });
        //供应商
        $("#SupName").bindSelect({
            url: "/OrderBill/GetSup",
            id: "id",
            text: "SupName"
        });

        form.render();
       
    });
    $(function () {
        var bodyheight = document.body.scrollHeight;
        $(".layui-card-body").css("height",bodyheight-55);
        $("#list1").jqGrid({
            styleUI: 'Bootstrap',
            datatype: "local",
            jsonReader: {
                repeatitems: false
            },
            colModel: [
                { label: 'ids', name: 'ids', index: 'ids', key: true, width: 75, hidden: true },
                {
                    label: '操作', name: 'Hand', index: 'Hand', width: 100, align: 'center', formatter: function (value, grid, rows, state) {
                        var detail = ' <button onclick="AddGoods(\'' + rows.ids + '\');" class="layui-btn layui-btn-xs" lay-event="AddInvoice">新增</button> <button onclick="DelRow(\'' + rows.ids + '\');" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="DelInvoice">删除</button>';
                        return detail;
                    }
                },
                { label: '商品id', name: 'Good_id', index: 'Good_id', width: 100, align: 'center', hidden: true},
                { label: '商品编号', name: 'GoodID', index: 'GoodID', width: 100, align: 'center', hidden: true },
                { label: '商品名称', name: 'GoodName', index: 'GoodName', width:140, align: 'center'},
                { label: '单位', name: 'DW', index: 'DW', width: 80, align: 'center' },
                { label: '规格', name: 'GGType', index: 'GGType', width: 100, align: 'center' },
                { label: '型号', name: 'ModelType', index: 'ModelType', width: 100, align: 'center' },
                { label: '生产厂家', name: 'SCCJ', index: 'SCCJ', width: 100, align: 'center' },
                { label: '数量', name: 'Num', index: 'Num', width: 100, align: 'center', editable: true },
                { label: '单价', name: 'Price', index: 'Price', width: 100, align: 'center', editable: true },
                { label: '金额', name: 'Sum', index: 'Sum', width: 100, align: 'center' },
                { label: '备注', name: 'BZ', index: 'BZ', width: 140, align: 'center', editable: true },

            ],
            width: "100%",
            autowidth: true,
            height: bodyheight-180,
            sortname: 'ID',//初始化的时候排序的字段
            sortorder: "desc",//排序方式,可选desc,asc
            //mtype: "post",//向后台请求数据的ajax的类型。可选post,get
            viewrecords: true,
            shrinkToFit: false,
            rownumbers: true,
            cellEdit: true,
            cellsubmit: 'clientArray',
            beforeEditCell: function (rowid, cellname, v, iRow, iCol) {
                lastrow = iRow;  //给全局变量赋值
                lastcell = iCol;
            }

          });
          //填充100行空行
          for (var i = 1; i <= 100; i++) {
              var item = {
                  "ids": i,
                  "Good_id": '',
                  "GoodID": '',
                  "GoodName": '',
                  "DW": '',
                  "GGType": '',
                  "ModelType": '',
                  "SCCJ": '',
                  "Price": '',
                  "Num": '',
                  "Sum": '',
                  "BZ": ''
              };
              $("#list1").jqGrid("addRowData", i, item, "last"); 
          }

    });
  
          
    //选择商品弹出框
    function AddGoods(ids) {
        var url = "/OrderBill/AddRowIndex" + "?ids=" + ids ;
        $.FromOpen('OrderBillFromAddRow', '选择商品信息', url, '600px', '500px', '1000');
    }
    //填充一行商品信息
    function AddRow(item) {
        $("#list1").jqGrid('setCell', item.ids, 'Good_id', item.Good_id);
        $("#list1").jqGrid('setCell', item.ids, 'GoodID', item.GoodID);
        $("#list1").jqGrid('setCell', item.ids, 'GoodName', item.GoodName);
        $("#list1").jqGrid('setCell', item.ids, 'DW', item.DW);
        $("#list1").jqGrid('setCell', item.ids, 'GGType', item.GGType);
        $("#list1").jqGrid('setCell', item.ids, 'ModelType', item.ModelType);
        $("#list1").jqGrid('setCell', item.ids, 'SCCJ', item.SCCJ);
        $("#list1").jqGrid('setCell', item.ids, 'Price', 0);
        $("#list1").jqGrid('setCell', item.ids, 'Num', 1);
        $("#list1").jqGrid('setCell', item.ids, 'Sum', 0);
    }
    //删除一行商品信息
    function DelRow(ids) {
        $("#list1").jqGrid('setCell', ids, 'Good_id', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'GoodID', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'GoodName', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'DW', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'GGType', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'ModelType', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'SCCJ', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'Price', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'Num', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'Sum', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'BZ', "&nbsp");
    }
    $(function (){
        $.hands = {
            SaveAdd: function () {
                var types = "no";
                var flag = $.hands.Add(types);
                if (flag) {
                    var rowdata = [];
                    for (var i = 1; i <= 100; i++) {
                        DelRow(i);
                    } 
                };
            },
            AddClose: function () {
                var types = "no";
                var flag = $.hands.Add(types);
                if (flag) {
                    top.$("#LAY_app_tabsheader").children(".layui-this").children(".layui-tab-close").trigger("click");
                };

            },
            AddSH: function () {
                var types = "yes";
                var flag = $.hands.Add(types);
                if (flag) {
                    top.$("#LAY_app_tabsheader").children(".layui-this").children(".layui-tab-close").trigger("click");
                };
            },
            Add: function (SHType) {
                $('#list1').jqGrid("saveCell", lastrow, lastcell);
                //选择日期判断
                var BillDate = $('#BillDate').val();
                if (BillDate == "" || BillDate == undefined || BillDate == null) {
                    layer.msg("请选择日期", { icon: 2 });
                    return false;
                }
                //选择采购员判断
                var CGNameID = $('#CGName').val();
                if (CGNameID == "" || CGNameID == undefined || CGNameID == null) {
                    layer.msg("请选择采购员", { icon: 2 });
                    return false;
                }
                var CGName = $("#CGName option:selected").text();
                //选择供应商判断
                var Sup_id = $('#SupName').val();
                if (Sup_id == "" || Sup_id == undefined || Sup_id == null) {
                    layer.msg("请选择供应商", { icon: 2 });
                    return false;
                }
                var SupName = $("#SupName option:selected").text();

                var BZ = $('#BZ').val();
                var AddData = [];
                var datas = $("#list1").jqGrid("getRowData"); 
                $.each(datas, function (i) {
                    var row = datas[i];
                    if (row.GoodName != "" && row.GoodName != undefined && row.GoodName != null) {
                        AddData.push(row);
                    }
                });
                if (AddData.length > 0) {
                    var Bill = {
                        "BillDate": BillDate,
                        "CGNameID": CGNameID,
                        "CGName": CGName,
                        "Sup_id": Sup_id,
                        "SupName": SupName,
                        "BZ": BZ
                    };
                    var flag = false;
                    $.loading(true, "正在提交数据");
                    $.ajax({
                        url: "/OrderBill/SaveOrderBill",
                        data: { SHTypes: SHType, OrderBiils: Bill, OrderBiils_MX: AddData },
                        dataType: "json",
                        type: "post",
                        async: false,
                        success: function (data) {
                            $.loading(false);
                            if (data.errorMsg) {
                                flag = false;
                                top.layer.msg(data.errorMsg, { icon: 2 });

                            }
                            else {
                                var msgs = (data.okMsg);
                                flag = true;
                                top.layer.msg(msgs, { icon: 1 });

                            }
                        },
                        error: function () {
                            $.loading(false);


                        },
                        complete: function () {
                            $.loading(false);

                        }
                    });
                    return flag;
                }
                else {
                    layer.msg("请输入商品信息", { icon: 2 });
                    return false;
                }

            },

            init:function() {
                $('#SaveAdd').on('click', $.hands.SaveAdd);
                $('#Add').on('click', $.hands.AddClose);
                $('#AddSH').on('click', $.hands.AddSH);

            }
        }
        $(function () {
            $.hands.init();
        });
    });
    //计算单价 数量 金额
    $(document).on('keyup', '#list1>tbody>tr>td>input[type="text"]', function () {
        var _inputName = $(this).attr('name');

        if (_inputName == "Price") {
            var id = $("#list1").jqGrid('getGridParam', 'selrow');//根据点击行获得点击行的id（id为jsonReader: {id: "id" },）
            var rowData = $("#list1").jqGrid("getRowData", id);
            var _price = Number($(this).val());
            if (isNaN(_price)) {
                _price = 0;
            }
            var _num = Number(rowData.Num);
            var _sum = $.MulNum(_price, _num);
            $("#list1").jqGrid('setCell', id, 'Sum', _sum);


        } else if (_inputName == "Num") {
            var id = $("#list1").jqGrid('getGridParam', 'selrow');//根据点击行获得点击行的id（id为jsonReader: {id: "id" },）
            var rowData = $("#list1").jqGrid("getRowData", id);
            var _price = Number(rowData.Price);
            var _num = Number($(this).val());
            if (isNaN(_num)) {
                _num = 0;
            }
            //var _sum = (Math.round(_price * 100) * _num) / 100;
            var _sum = $.MulNum(_price, _num);
            $("#list1").jqGrid('setCell', id, 'Sum', _sum);

        }
    });
  

</script>
<style>
    .form-control {
        height: 20px;
        text-align: center;
        display: table-cell;
        vertical-align: middle
    }
</style>
